

import os
import re

# Replace 'directory_path' with the path to the directory you want to search
d_directory_path = './download_dir/pastnotes1982/WEB/HTML'
u_directory_path = './download_dir/pastnotes1982/WEB/HTML'

# Define the regular expression pattern
pattern = re.compile(r'<style>.*themes.googleusercontent.*</style>', re.DOTALL)


# Define the string to replace the matched pattern with
replacement_string = '<style> body { background-image: url("../IMAGES/woodb.jpg") } </style>'
 

# Loop through each file in the directory
for filename in os.listdir(d_directory_path):
    # Check if the file extension is .htm
    if (filename.endswith('.HTM') and (not filename.startswith('VIDEO'))):
        # Open the file and read its contents
        pathnamef = os.path.join(d_directory_path, filename) 
        print (pathnamef)
        
        with open(pathnamef, 'r') as file:
            contents = file.read()
            
            # Check if the regular expression matches the contents
            if pattern.search(contents):
                print(f"{filename} pattern found")
                # Replace the matched pattern with the replacement string
                new_contents = pattern.sub(replacement_string, contents)
                
                # Write the new contents back to the file
                with open(os.path.join(u_directory_path, filename), 'w') as new_file:
                    new_file.write(new_contents)                    
                    print(f"{filename} updated")
            else:
                print(f"{filename} does not contain the pattern")
                
        
